package my.example.samplegridview;

import java.util.Vector;

import my.example.samplegridview.modal.product;

public class currentcartitems {
	public static Vector<product> cartitems = new Vector<product>();
	public static void addToCurrentCartItems(product p){
		/*for(int i=0;i<cartitems.size();i++){
			if(cartitems.get(i).getProduct_id().compareTo(p.getProduct_id()) == 0){
				boolean isAllSame = true;
				for(int i2 = 0 ; i2 < cartitems.get(i).getProduct_options(true).size() ; i2++){
					if(cartitems.get(i).getProduct_options(true).get(i2).getSelectedoption() != p.getProduct_options(true).get(i2).getSelectedoption())
						isAllSame = false;
				}
				if(isAllSame)
					cartitems.remove(i);
			}
		}*/
		cartitems.add(p);
	}
	public static void editCurrentCartItems(int location , product p){
		cartitems.set(location, p);
	}
	public static void removeCartItem(int location){
		cartitems.remove(location);
	}
	public static double totalCartPrice(){
		double totalprice = 0;
		for(int i=0;i<cartitems.size();i++){
			totalprice += Double.parseDouble(cartitems.get(i).getPrice()) * Double.parseDouble(cartitems.get(i).getOrderedQuantity());
			for(int i1=0;i1<cartitems.get(i).getProduct_options(true).size();i1++){
				int selectoption = cartitems.get(i).getProduct_options(true).get(i1).getSelectedoption();
				totalprice += Double.parseDouble(cartitems.get(i).getOrderedQuantity()) * Double.valueOf(cartitems.get(i).getProduct_options(true).get(i1).getProduct_option_values(true).get(selectoption).getPrice());
			}
		}
		return totalprice;
	}
	public static void clearCart(){
		int totalItems = cartitems.size();
		for(int i=0;i<totalItems;i++){
			if(cartitems.get(i).getProductStatus().compareTo("Not placed") == 0){
				cartitems.remove(i);
				i--;
				totalItems--;
			}
		}
	}
	
	//Placed  /  Not Placed
	public static void sortingCurrentCartItem(){
		Vector<product> placed = new Vector<product>();
		Vector<product> notplaced = new Vector<product>();
		for(int i=0;i<cartitems.size();i++){
			if(cartitems.get(i).getProductStatus().compareTo("Placed") == 0){
				placed.add(cartitems.get(i));
			}
			else if(cartitems.get(i).getProductStatus().compareTo("Not placed") == 0){
				notplaced.add(cartitems.get(i));
			}
		}
		cartitems.removeAllElements();
		cartitems.addAll(notplaced);
		cartitems.addAll(placed);
	}
	public static boolean cartContainedUnOrderedItem(){
		boolean containUnOrderedItem = false;
		if(cartitems.size() > 0){
			for(int i=0;i<cartitems.size();i++){
				if(cartitems.get(i).getProductStatus().compareTo("Not placed") == 0){
					containUnOrderedItem = true;
					break;
				}
			}
		}
		return containUnOrderedItem;
	}
	public static void turnAllProductStatus(String status){
		for(int i=0;i<cartitems.size();i++){
			cartitems.get(i).setProductStatus(status);
		}
	}
}
